package com.qzdsoft.erpcloud.controller.act;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.service.act.CenterCompanyService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysLoginService;
import com.qzdsoft.erpcloud.service.sys.SysRoleService;
import com.qzdsoft.erpcloud.vo.query.act.CenterCompanyInfo;
import com.qzdsoft.erpcloud.vo.query.act.CenterCompanyQueryParam;
import com.qzdsoft.utils.SysUtils;
import com.qzdsoft.vo.EnumPo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.sys.SysUserStatusEnum;

/**
 * Created by Yang
 */
@Controller
@RequestMapping("/act/center/company")
public class CenterCompanyController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(CenterCompanyController.class);
    
    @Autowired
    private CenterCompanyService  actCenterCompanyService;
    
    @Autowired
    private SysLoginService sysLoginService;

//    @Autowired
//    private RedisHelper redisHelper;
    
    @Autowired
    SysRoleService sysRoleService;

    /**
     * 首页
     * @param request
     * @param model
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/index")
    public Object index(HttpServletRequest request, Model model) {
        List<EnumPo> statusList = new ArrayList<EnumPo>();
        Map<Object, Object> statusMap = new HashMap<Object, Object>();
        for(SysUserStatusEnum e : SysUserStatusEnum.values()){
            SysUtils.enums(statusList, statusMap, e.getKey(), e.getVal());
        }
        model.addAttribute("statusList", statusList);
        model.addAttribute("statusMap", statusMap);
        model.addAttribute("sysRoleMap", sysRoleService.getAllRoleList());
        return "act/company/center_company2";
    }

    /**
     * 列表查询数据
     * @param param     封装查询参数
     * @return
     */
    @ResponseBody
    @SysLog(description="账户信息查询")
    @RequestMapping("/data")
    public LayTableResponse<CenterCompanyInfo> list(CenterCompanyQueryParam param){
            return actCenterCompanyService.selectData(param.getName(), param.getEmail(), param.getContactUser(), param.getContactPhone(), param.getStatus(), param.getTimeFrom(), param.getTimeTo(), param.getRecommendName(), param.getRoleName(),param.getPage(),param.getLimit());
    }

    /**
     * 编辑数据
     * @param request
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/edit")
    public ModelAndView edit(Integer companyId,Integer id) {
        ModelAndView mv = new ModelAndView();
        List<EnumPo> statusList = new ArrayList<EnumPo>();
        Map<Object, Object> statusMap = new HashMap<Object, Object>();
        for(SysUserStatusEnum e : SysUserStatusEnum.values()){
            SysUtils.enums(statusList, statusMap, e.getKey(), e.getVal());
        }
        mv.addObject("statusList", statusList);
        mv.addObject("statusMap", statusMap);
        
        mv.addObject("sysRoleMap", sysRoleService.getAllRoleList());
        if(id == null) {
            mv.setViewName("act/company/add");
        }else {
            CenterCompanyInfo centerCompany = actCenterCompanyService.getByOneId(id);
            mv.addObject("centerCompany", centerCompany);
            mv.setViewName("act/company/edit");
        }
        return mv;
    }
    /**
     *
     * @param request
     * @param actCenterCompany
     * @return
     */
    @ResponseBody
    @SysLog(description="账户信息修改保存")
    @RequestMapping("/edit_execute")
    public Response<String> editExecute(CenterCompanyInfo actCenterCompanyInfo) {
        logger.debug("账户信息修改保存"+actCenterCompanyInfo);
        return actCenterCompanyService.edit(actCenterCompanyInfo);
    }
    /**
     *
     * @param request
     * @param actCenterCompany
     * @return
     */
    @ResponseBody
    @SysLog(description="账户信息新增保存")
    @RequestMapping("/create_execute")
    public Response<String> createExecute(CenterCompanyInfo actCenterCompanyInfo) {
        logger.debug("账户信息新增保存"+actCenterCompanyInfo);
        return actCenterCompanyService.edit(actCenterCompanyInfo);
    }
    
    
    
    /**
     * 修改密码
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/alretPwd")
    public ModelAndView alertPwd(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("id",id);
        mv.setViewName("act/company/alretPwd");
        return mv;
    }
    /**
     * 修改密码保存
     * @param info
     * @return
     */
    @ResponseBody
    @SysLog(description="账户信息修改密码")
    @RequestMapping("/pwdSave")
    public Response<String> save(String phone,String opd,String npd){
        logger.debug("账户信息修改密码:"+phone);
        if(phone==null || "".equals(phone)) {
            phone=this.getUser().getPhone();
        }
        return actCenterCompanyService.pwdAlter(phone, opd, npd);
    }


    /**
     * 详情
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/detail")
    public ModelAndView detail(Integer id) {
        ModelAndView mv = new ModelAndView();
        logger.debug("账户信息详情："+id);
        CenterCompanyInfo centerCompanyinfo = actCenterCompanyService.getByOneId(id);
        mv.addObject("centerCompany",centerCompanyinfo);
        mv.setViewName("act/company/view");
        return mv;
    }

    /**
     * 根据主键删除
     * @param request
     * @param id
     * @return
     */
    @ResponseBody
    @SysLog(description="账户信息删除")
    @RequestMapping("/del_execute")
    public Response<String> delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        logger.debug("账户信息删除id: "+id);
        return actCenterCompanyService.del(id);
    }
    
    

}
